<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>投票系统</title>
    <meta name="viewport" content="width=device-width"/>
    <script src="https://unpkg.com/jquery"></script>
    <script src="https://surveyjs.azureedge.net/1.8.12/survey.ko.min.js"></script>
    <link href="https://surveyjs.azureedge.net/1.8.12/modern.css" type="text/css" rel="stylesheet" />
    <script src="https://surveyjs.azureedge.net/1.8.12/survey.jquery.min.js"></script>
</head>
<body id="main">
    <div id="context" d="{{content}}" style="display:none"></div>
    <div id="code" d="{{code}}" style="display:none"></div>
    <div id="table_id" d="{{table_id}}" style="display:none"></div>
    <div id="surveyContainer"></div>    
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/sweetalert/2.1.0/sweetalert.min.js"></script>
<script>
    Survey.StylesManager.applyTheme("modern");

    // var surveyJSON = {"pages":[{"name":"page1","elements":[{"type":"text","name":"question1"}]},{"name":"page2","elements":[{"type":"checkbox","name":"question2","choices":["item1","item2","item3"]}]},{"name":"page3","elements":[{"type":"radiogroup","name":"question3","choices":["item1","item2","item3"]}]}]}
    var js_data = document.getElementById('context').getAttribute('d');
    var surveyJSON = js_data


    function sendDataToServer(survey) {
        var code = document.getElementById('code').getAttribute('d');
        var table_id = document.getElementById('table_id').getAttribute('d');

        data = {
            'code':code,'data':JSON.stringify(survey.data),'table_id':table_id
        }

        $.ajax({
            type: "post",
            url: "http://127.0.0.1:5000/submit",
            data: data,
            dataType: "json",
            success: function (response) {
                // return {"code":200,"message":"提交成功!"}
                if(response.code == 200 || response.code == '200'){
                    swal("完成", response.message, "success");
                }else{
                    swal("失败", response.message, "error");
                }
            },
            error:function(err){
                swal("系统错误!", "请稍后再试!", "error");

            }
        });


        alert("The results are:" + JSON.stringify(survey.data));
    }

    var survey = new Survey.Model(surveyJSON);
    $("#surveyContainer").Survey({
        model: survey,
        onComplete: sendDataToServer
    });
</script>
</html>